package com.aegonthtf.sis.aspect;


import cn.com.common.util.domain.RequestStore;
import cn.com.common.util.page.CustomPage;
import cn.com.common.util.util.Assert;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 分页切面
 *
 * @author huangjiangming@fulan.com.cn
 * @date 2021/4/6 9:16
 */
@Aspect
@Component
@Order(20)
public class PageAspect {

    private static String TOTAL_COUNT = "totalCount";
    private static String PAGE_NUM = "pageNum";
    private static String PAGE_SIZE = "pageSize";
    private static String PAGE_COUNT = "pageCount";
    private static Integer DEFAULT_PAGE_NUM = 1;
    private static Integer DEFAULT_PAGE_SIZE = 10;

    /**
     * 分页处理器
     *
     * @param joinPoint 切面点
     * @return java.lang.Object
     * @author huangjiangming@fulan.com.cn
     * @date 2021/4/6 9:23
     */
    @Around("execution(* com.aegonthtf.sis..controller..find*Page(..))")
    public Object pageHandler(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        if (CollectionUtils.isNotEmpty(Arrays.asList(args))) {
            if (args[0] instanceof CustomPage) {
                // 初始化分页参数
                JSONObject reqData = JSONObject.parseObject(JSONObject.toJSONString(args[0]));
                Integer pageNum = reqData.getInteger("pageNum");
                Integer pageSize = reqData.getInteger("pageSize");
                if(pageNum == null || pageNum == 0){
                    pageNum = DEFAULT_PAGE_NUM;
                }
                if(pageSize == null || pageSize == 0){
                    pageSize = DEFAULT_PAGE_SIZE;
                }
                RequestStore.setPageNum(pageNum);
                RequestStore.setPageSize(pageSize);
            }
        }
        Assert.notNull(RequestStore.getPageNum(), "页码不能为空");
        Assert.notNull(RequestStore.getPageSize(), "每页大小不能为空");
        Page<Object> page = PageHelper.startPage(RequestStore.getPageNum(), RequestStore.getPageSize());
        RequestStore.setPage(page);
        // 处理业务逻辑
        Object object = joinPoint.proceed(args);
        // 设置总记录数
        BeanWrapper beanWrapper = new BeanWrapperImpl(object);
        // 此处page分页对象不从方法变量获取，用于兼容分页方法中有多个查询的写法
        beanWrapper.setPropertyValue(TOTAL_COUNT, RequestStore.getPage().getTotal());
        beanWrapper.setPropertyValue(PAGE_NUM, RequestStore.getPage().getPageNum());
        beanWrapper.setPropertyValue(PAGE_SIZE, RequestStore.getPage().getPageSize());
        beanWrapper.setPropertyValue(PAGE_COUNT, RequestStore.getPage().getPages());
        return object;
    }

}
